//
// BrowseDirPage
//
// Copyright (C) jextra.net.
//
//  This file is part of the Nubbin Library.
//
//  The Nubbin Library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The Nubbin Library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the Nubbin Library; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.nubbinstoreweb;

import com.javapepper.core.*;
import com.javapepper.page.*;
import com.javapepper.widget.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.http.*;
import net.jextra.nubbin.store.*;

public class BrowseDirPage extends PepPage
{
    // ============================================================
    // Fields
    // ============================================================

    private ConfigModel _model;

    private DateFormat _df;
    private StorePathMapper _pathMap;
    private ArrayList<PepWidget> _widgets;
    private int _row;

    // ============================================================
    // Constructors
    // ============================================================

    public BrowseDirPage()
        throws IOException
    {
        _df = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public void init( ConfigModel model )
    {
        _model = model;
    }

    @Override
    public Collection<PepWidget> buildWidgets( PepSession pepSession, HttpServletRequest request,
        HttpServletResponse response )
        throws Exception
    {
        addStyleSheet( "/nubbinstore/css/nubbinstore.css" );

        setTitle( "Nubbin Store : " + request.getServletPath() );
        _widgets = new ArrayList<PepWidget>();

        PepPanel links = new PepPanel( "links" );
        links.setStyleClass( "links" );
        _widgets.add( links );
        links.add( new PepLink( "/nubbinstore", "Goto NubbinStore Home Page" ) );

        if ( _model.getStore() == null )
        {
            _widgets.add( new PepParagraph( "NubbinStore-Web is not configured to be a store." ) );
            return _widgets;
        }

        _pathMap = new StorePathMapper( request );
        _row = 0;
        switch ( _pathMap.getType() )
        {
            case components:
                listComponents();
                break;

            case versions:
                listVersions();
                break;

            default:
                listEntries();
                break;
        }

        return _widgets;
    }

    // ----------
    // private
    // ----------

    private void listComponents()
        throws Exception
    {
        _widgets.add( new PepHeader( 1, "Nubbin Store Components" ) );

        PepTable table = new PepTable();
        table.setStyleClass( "dirTable" );
        _widgets.add( table );

        for ( NubbinStoreComponent component : _model.getStore().getComponents() )
        {
            addItem( table, "/nubbinstore/browse/" + component.getComponentName(),
                component.getComponentName() + "/", component.getTimestamp(), "-" );
        }

    }

    private void listVersions()
        throws Exception
    {
        _widgets.add( new PepHeader( 1, "Nubbin Store Component Versions : " +
            _pathMap.getComponentName() ) );

        PepTable table = new PepTable();
        table.setStyleClass( "dirTable" );
        _widgets.add( table );

        addItem( table, "/nubbinstore/browse", "../", 0L, "" );

        for ( NubbinStoreVersion version : _model.getStore().getVersions(
            _pathMap.getComponentName() ) )
        {
            addItem( table, "/nubbinstore/browse/" + version.getComponentName() + "/" +
                version.getVersionNumber(), version.getVersionNumber() + "/",
                version.getTimestamp(), "-" );
        }

    }

    private void listEntries()
        throws Exception
    {
        _widgets.add( new PepHeader( 1, "Nubbin Store Entries : " + _pathMap.getComponentName() +
            "/" + _pathMap.getVersionNumber() + ( _pathMap.getPath().isEmpty() ? "" : "/" +
            _pathMap.getPath() ) ) );

        PepTable table = new PepTable();
        table.setStyleClass( "dirTable" );
        _widgets.add( table );

        String parentPath = _pathMap.getPath();
        int slash = parentPath.lastIndexOf( "/" );
        if ( parentPath.isEmpty() )
        {
            parentPath = "";
        }
        else if ( slash >= 0 )
        {
            parentPath = "/" + _pathMap.getVersionNumber() + "/" + parentPath.substring( 0, slash );
        }
        else
        {
            parentPath = "/" + _pathMap.getVersionNumber();
        }
        addItem( table, "/nubbinstore/browse/" + _pathMap.getComponentName() + parentPath, "../", 0L,
            "" );

        for ( NubbinStoreEntry entry : _model.getStore().getEntries( _pathMap.getComponentName(),
            _pathMap.getVersionNumber(), _pathMap.getPath() ) )
        {
            String path = entry.getPath();
            slash = path.lastIndexOf( '/' );
            String name = slash >= 0 ? path.substring( slash + 1 ) : path;
            String suffix = entry.isDirectory() ? "/" : "";
            addItem( table, "/nubbinstore/browse/" + entry.getComponentName() + "/" +
                entry.getVersionNumber() + "/" + entry.getPath(), name + suffix,
                entry.getTimestamp(), entry.isDirectory() ? "-" : "" + entry.getSize() );
        }
    }

    private void addItem( PepTable table, String url, String name, long timestamp, String size )
    {
        table.setBucketSpecificStyleClass( 0, _row, "fill" );
        table.add( 0, _row, new PepLink( url, name ) );
        if ( timestamp > 0L )
        {
            Date date = new Date( timestamp );
            table.add( 1, _row, new PepText( _df.format( date ) ) );
        }

        if ( size != null )
        {
            table.add( 2, _row, new PepText( size ) );
        }

        table.setRowSpecificStyleClass( _row, _row % 2 == 0 ? "even" : "odd" );

        _row++;
    }
}

